package com.weigecoder.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weigecoder.auth.mapper.SysUserMapper;
import com.weigecoder.auth.service.*;
import com.weigecoder.model.system.*;
import com.weigecoder.security.custom.LoginUserInfoHelper;
import com.weigecoder.vo.system.SysUserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户表 服务实现类
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysPostService sysPostService;

    //更新状态
    @Override
    public void updateStatus(Long id, Integer status) {
        //根据userid查询用户对象
        SysUser sysUser = baseMapper.selectById(id);
        //设置修改状态值
        sysUser.setStatus(status);
        //调用方法进行修改
        baseMapper.updateById(sysUser);
    }

    //根据用户名进行查询
    @Override
    public SysUser getUserByUserName(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != username, SysUser::getUsername, username);
        SysUser sysUser = baseMapper.selectOne(wrapper);
        return sysUser;
    }

    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = baseMapper.selectById(LoginUserInfoHelper.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getName());
        map.put("phone", sysUser.getPhone());
        return map;
    }

    @Override
    public IPage<SysUser> findSysUserPage(Page<SysUser> pageParam, SysUserQueryVo sysUserQueryVo) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        //获取条件值
        String keyword = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        //判断条件值不为空
        //like 模糊查询
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like(SysUser::getUsername, keyword)
                    .or()
                    .like(SysUser::getPhone, keyword)
                    .or()
                    .like(SysUser::getName, keyword);
        }
        //ge 大于等于
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
        //le 小于等于
        if (!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le(SysUser::getCreateTime, createTimeEnd);
        }

        IPage<SysUser> pageModel = baseMapper.selectPage(pageParam, wrapper);

        List<SysUser> sysUserList = pageModel.getRecords();
        for (SysUser sysUser : sysUserList) {
            LambdaQueryWrapper<SysUserRole> sysUserRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysUserRoleLambdaQueryWrapper.eq(SysUserRole::getUserId, sysUser.getId());
            List<SysUserRole> sysUserRoles = sysUserRoleService.list(sysUserRoleLambdaQueryWrapper);
            List<Long> collect = sysUserRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
            List<SysRole> roleList = new ArrayList<>();
            if (!Objects.isNull(collect)) {
                for (Long roleId : collect) {
                    SysRole sysRole = sysRoleService.getById(roleId);
                    roleList.add(sysRole);
                }
                sysUser.setRoleList(roleList);
            } else {
                sysUser.setRoleList(null);
            }
            Long deptId = sysUser.getDeptId();
            Long postId = sysUser.getPostId();
            if (!Objects.isNull(deptId)) {
                SysDept sysDept = sysDeptService.getById(deptId);
                sysUser.setDeptName(sysDept.getName());
            } else {
                sysUser.setDeptName("暂无");
            }
            if (!Objects.isNull(postId)) {
                SysPost sysPost = sysPostService.getById(postId);
                sysUser.setPostName(sysPost.getName());
            } else {
                sysUser.setPostName("暂无");
            }
        }
        return pageModel;
    }
}
